package main

import (
	"fmt"
	"interstellar/zinx/ziface"
	"interstellar/zinx/znet"
)

/*
	基于Zinx框架来开发的 服务器端应用程序
*/

// PingRouter ping test 自定义路由
type PingRouter struct {
	znet.BaseRouter
}

//Test Handle
func (this *PingRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call Router Handle...")
	// 先读取客户端的数据，在会写ping..ping...ping
	fmt.Println("recv form client: msgID = ", request.GetMsgID(),
		", data = ", string(request.GetData()))

	err := request.GetConnection().SendMsg(1, []byte("ping...ping...ping"))
	if err != nil {
		fmt.Println(err)
	}
}

// HelloRouter ping test 自定义路由
type HelloRouter struct {
	znet.BaseRouter
}

//Test Handle
func (this *HelloRouter) Handle(request ziface.IRequest) {
	fmt.Println("Call HelloRouterHandle Handle...")
	// 先读取客户端的数据，在会写ping..ping...ping
	fmt.Println("recv form client: msgID = ", request.GetMsgID(),
		", data = ", string(request.GetData()))

	err := request.GetConnection().SendMsg(201, []byte("Hello Welcome to Zinx!"))
	if err != nil {
		fmt.Println(err)
	}
}

// DoConnectionBegin 创建链接之后执行钩子函数
func DoConnectionBegin(conn ziface.IConnection) {
	fmt.Println("===> DoConnectionBegin is Called ...")
	if err := conn.SendMsg(202, []byte("DoConnection BEGIN")); err != nil {
		fmt.Println(err)
	}

	// 给当前的链接设置一些属性
	fmt.Println("Set conn property...")
	conn.SetProperty("Name", "某某某-哈哈哈")
	conn.SetProperty("GitHub", "https://github.com/aceld")
	conn.SetProperty("Home", "https://legacy.gitbook.com/@aceld")
	conn.SetProperty("Blog", "https://www.jianshu.com/u/35261429b7f1")
}

// DoConnectionLost 链接端口之前的需要执行的函数
func DoConnectionLost(conn ziface.IConnection) {
	fmt.Println("===> DoConnectionLost is Called...")
	fmt.Println("conn ID = ", conn.GetConnId(), " is Lost...")

	// 获取链接属性
	if name, err := conn.GetProperty("Name"); err == nil {
		fmt.Println("Name = ", name)
	}
	if home, err := conn.GetProperty("Home"); err == nil {
		fmt.Println("Home = ", home)
	}
	if github, err := conn.GetProperty("GitHub"); err == nil {
		fmt.Println("GitHub = ", github)
	}
	if blog, err := conn.GetProperty("Blog"); err == nil {
		fmt.Println("Blog = ", blog)
	}
}

func main() {
	//1 创建一个server句柄，使用Zinx的api
	s := znet.NewServer("[zinx V0.1]")

	//2 注册链接Hook钩子函数
	s.SetOnConnStart(DoConnectionBegin)
	s.SetOnConnStop(DoConnectionLost)

	//3 给当前zinx框架添加一个自定义的router
	s.AddRouter(0, &PingRouter{})
	s.AddRouter(1, &HelloRouter{})

	//4 启动server
	s.Serve()
}
